package com.rzhd.coursepatriarch.common.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.StrictMode;
import android.support.v4.content.FileProvider;

import com.rzhd.coursepatriarch.common.BuildConfig;

import java.io.File;

/**
 * Created by CL on 2017/9/11.
 */

public class OpenFiles {

    public static Intent getAutoFileIntent( Context context , String Path){
        File file = new File(Path);
        String fileName = file.getName();
        String prefix = fileName.substring(fileName.lastIndexOf(".")+1);
        //==================2019-4-1新增===============
        //转换成大写
        prefix = prefix.toUpperCase();
        Intent intent = null;
        switch (FileType.valueOf(prefix)){
            case TEXT:
                intent = getTextFileIntent(file.getPath());
                break;
            case TXT:
                intent = getTextFileIntent(file.getPath());
                break;
            case IMAGE:
                intent = getImageFileIntent(file.getPath());
                break;
            case JPEG:
                intent = getImageFileIntent(file.getPath());
                break;
            case PNG:
                intent = getImageFileIntent(file.getPath());
                break;
            case JPG:
                intent = getImageFileIntent(file.getPath());
                break;
            case ZIP:
                intent = getZipRarFileIntent(file.getPath());
                break;
            case RAR:
                intent = getZipRarFileIntent(file.getPath());
                break;
            case PPT:
                intent = getPPTFileIntent(file.getPath());
                break;
            case PDF:
                intent = getPdfFileIntent(file.getPath());
                break;
            case XLS:
            case XLSX:
                intent = openFileEx( context , file.getPath() ,  "application/vnd.ms-excel" );
                break;
            case DOC:
            case DOCX:
                intent = openFileEx( context , file.getPath() , "application/msword" );
                break;
        }

        return intent;
    }

    public static Intent openFileEx( Context context , String filePath, String fileType ) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        // 判断版本大于等于7.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
        Uri data = Uri.fromFile(new File(filePath));
        intent.setDataAndType(data, fileType);
//        context.startActivity(intent);
        return intent;
    }

    enum FileType{
        TEXT , TXT , HTML , IMAGE , JPEG , PNG , JPG , ZIP , RAR , PPT , PDF ,XLS, XLSX , DOC,DOCX
    }

    //android获取一个用于打开HTML文件的intent
    public static Intent getHtmlFileIntent(String Path)
    {
        File file = new File(Path);
        Uri uri = Uri.parse(file.toString()).buildUpon().encodedAuthority("com.android.htmlfileprovider").scheme("content").encodedPath(file.toString()).build();
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.setDataAndType(uri, "text/html");
        return intent;
    }
    //android获取一个用于打开图片文件的intent
    public static Intent getImageFileIntent(String Path)
    {
        File file = new File(Path);
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "image/*");
        return intent;
    }


    //android获取一个用于打开PDF文件的intent
    public static Intent getPdfFileIntent(String Path){
        File file = new File(Path);
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/pdf");
        return intent;
    }

    /**
     * 查看表格文件
     * @param Path
     * @return
     */
    public static Intent getXlsFileIntent(Context context , String Path , String fileType ){
        File file = new File(Path);
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/vnd.ms-excel");
        return intent;
    }

    /**
     * 查看文件
     * @param Path
     * @return
     */
    public static Intent getFileIntent(Context context , String Path , String fileType ){

        Intent intent = new Intent("android.intent.action.VIEW");
        try {
            intent.addCategory("android.intent.category.DEFAULT");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri uri = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileProvider", new File(Path));
            } else {
                uri = Uri.fromFile(new File(Path));
            }
            intent.setDataAndType(uri, fileType );
        }catch ( Exception e ){
            e.printStackTrace();
        }
        return intent;
    }

    /**
     * 查看doc文件
     * @param Path
     * @return
     */
    public static Intent getDocFileIntent(String Path){
        File file = new File(Path);
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/msword");
        return intent;
    }


    //android获取一个用于打开文本文件的intent
    public static Intent getTextFileIntent(String Path){
        File file = new File(Path);
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "text/plain");
        return intent;
    }

    //android获取一个用于打开音频文件的intent
    public static Intent getAudioFileIntent(String Path){
        File file = new File(Path);
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("oneshot", 0);
        intent.putExtra("configchange", 0);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "audio/*");
        return intent;
    }

    //android获取一个用于打开视频文件的intent
    public static Intent getVideoFileIntent(String Path){
        File file = new File(Path);
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra("oneshot", 0);
        intent.putExtra("configchange", 0);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "video/*");
        return intent;
    }


    //android获取一个用于打开CHM文件的intent
    public static Intent getChmFileIntent(String Path){
        File file = new File(Path);
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/x-chm");
        return intent;
    }


    //android获取一个用于打开Word文件的intent
    public static Intent getWordFileIntent(String Path){
        File file = new File(Path);
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/msword");
        return intent;
    }

    //android获取一个用于打开Excel文件的intent
    public static Intent getExcelFileIntent(String Path){
        File file = new File(Path);
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/vnd.ms-excel");
        return intent;
    }

    //android获取一个用于打开PPT文件的intent
    public static Intent getPPTFileIntent(String Path){
        File file = new File(Path);
        Intent intent = new Intent("android.intent.action.VIEW");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
        return intent;
    }

    //android获取一个用于打开apk文件的intent
    public static Intent getApkFileIntent(String Path){
        File file = new File(Path);
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
        return intent;
    }

    //android获取一个用于打开zip跟rar文件的intent
    public static Intent getZipRarFileIntent(String Path){
        File file = new File(Path);
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file),  "application/x-gzip");
        return intent;
    }

}
